﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.WarehouseInsp.Buz.Interfaces;
using Microhard.Core.DBUtility;
using Microhard.WarehouseInsp.Buz.DataAccess;
using Microhard.WarehouseInsp.Buz.DTO;
using Microhard.WarehouseInsp.Buz.BuzEnum;
using Microhard.Core;
using Microhard.WarehouseInsp.Buz.Msg;
using Microhard.WarehouseInsp.Buz.Models;

namespace Microhard.WarehouseInsp.Buz.Impls
{
    public class SuprjDetailImpl : ISuprjWarnDetail
    {
        public Core.DBUtility.ExecutedResult Add(Models.SupervisionDetailModel model)
        {
            ExecutedResult rtn = new ExecutedResult();
            WarehouseInspEntities db = new WarehouseInspEntities();
            var op = model.ToEntity();
            op.CreateTime = DateTime.Now;
            op.Status = (int)PrjStatus.Saved;

            db.SupervisionDetails.AddObject(op);
            try
            {
                db.SaveChanges();
            }
            catch (System.Exception ex)
            {
                rtn.ErrorMsg.Add(DBOperationFailedStatus.AddFailed, SupervisionDetailMsg.AddFailed);
                Log.Error("ISuprjWarnDetail.Add", ex);
            }
            return rtn;
        }

        public Core.DBUtility.ExecutedResult Update(Models.SupervisionDetailModel model)
        {
            ExecutedResult result = new ExecutedResult();
            WarehouseInspEntities db = new WarehouseInspEntities();
            try
            {
                var entity = db.SupervisionDetails.SingleOrDefault(l => l.DetailID == model.DetailID);
                if (entity != null)
                {
                    entity.Worth = model.Worth;
                    entity.WarnAmount = model.WarnAmount;
                    entity.Guardline = model.Guardline;
                    entity.CreateTime = DateTime.Now;
                }
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                result.ErrorMsg.Add(DBOperationFailedStatus.UpdateFailed, RiskRecordsMsg.UpdateFailed);
                Log.Error("IRiskRecords.Update", ex);
            }
            return result;
        }

        public Core.DBUtility.ExecutedResult Delete(List<Guid> ids)
        {
            ExecutedResult result = new ExecutedResult();
            WarehouseInspEntities db = new WarehouseInspEntities();
            var toDelete = db.SupervisionDetails.Where(e => ids.Contains(e.DetailID)).ToList();
            toDelete.ForEach(e =>
            {
                //用户是否在使用
                if (e.Status == (int)PrjStatus.Saved)
                {
                    db.SupervisionDetails.DeleteObject(e);
                }
                else
                {
                    result.ErrorMsg.Add(DBOperationFailedStatus.DeleteFailed, string.Format(SubProjectMsg.IsInUsing, e.Guardline, e.WarnAmount));
                }
            });
            if (result.ErrorMsg.Count == 0)
            {
                try
                {
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    result.ErrorMsg.Add(DBOperationFailedStatus.DeleteFailed, SupervisionDetailMsg.DeleteFailed);
                    Log.Error("ISuprjWarnDetail.Delete", ex);
                }
            }
            return result;
        }


        public Core.DBUtility.ExecutedResult Delete(Models.SupervisionDetailModel model)
        {
            throw new NotImplementedException();
        }

        public Models.SupervisionDetailModel GetModelByID(Guid id)
        {
            throw new NotImplementedException();
        }

        public Models.SupervisionDetailModel GetModelByModel(Models.SupervisionDetailModel model)
        {
            WarehouseInspEntities db = new WarehouseInspEntities();
            var m = db.SupervisionDetails.Where(n => n.ProjectID == model.ProjectID && n.Status == (int)PrjStatus.Finished);

            SupervisionDetailModel sp = m.OrderByDescending(n => n.CreateTime).FirstOrDefault().ToModel();
            return sp;
        }

        public List<Models.SupervisionDetailModel> Query(Models.SupervisionDetailModel Model, int pageIndex, int pageSize, out int totalCount)
        {
            throw new NotImplementedException();
        }

        public List<Models.SupervisionDetailModel> Query(Models.SupervisionDetailModel Model)
        {
            WarehouseInspEntities db = new WarehouseInspEntities();
            IEnumerable<SupervisionDetail> entities = from b in db.SupervisionDetails
                                                      where b.ProjectID == Model.ProjectID
                                                      select b;
            var pagedEntities = entities.OrderByDescending(p => p.CreateTime).ToList();
            if (pagedEntities != null && pagedEntities.Count > 0)
                return pagedEntities.Select(m => m.ToModel()).ToList();
            return new List<SupervisionDetailModel>();
        }

        public Core.DBUtility.ScopeResult scope
        {
            set;
            get;
        }
    }
}
